﻿import com.dinmax.flash.gsa.communication.message.*
import com.dinmax.flash.gsa.communication.action.*
import com.dinmax.flash.gsa.communication.action.machine.*
import com.dinmax.flash.gsa.communication.action.player.*
import com.dinmax.flash.gsa.communication.action.game.*
import com.dinmax.flash.gsa.communication.parameter.*
import com.dinmax.flash.gsa.communication.*
import com.dinmax.flash.gsa.exception.*
import com.dinmax.flash.gsa.exception.communication.*
import com.dinmax.flash.gsa.sgs.state.*
import com.dinmax.flash.gsa.sgs.player.*
import com.dinmax.flash.gsa.sgs.game.*
import com.dinmax.flash.gsa.sgs.*
import com.dinmax.flash.utilities.timer.*

class Communication 
extends XMLSocket
implements TimerListener {
	
private static var instance:Communication;

private var listener:CommunicationEventListener;
private var IP:String;
private var PORT:Number;

public static function getActive():Communication {
	//trace("Communication.as | getActive");
	return instance;
}


public static function newInstance(listener:CommunicationEventListener):Void {
	//trace("Communication.as | newInstance "+listener);
	instance=new Communication(listener);
}


function Communication(listener:CommunicationEventListener) {
	this.listener=listener;
	var root = this;
	var ipLoader:LoadVars = new LoadVars();
	ipLoader.onLoad = function(){
		delete this.onLoad;
		root.IP=this.IP;
		root.PORT=this.PORT;
		root.doCommunication();
	}
	ipLoader.load("ip");
}


public function doCommunication():Boolean {
	return super.connect(IP,PORT);
}; //fin función


public function registerMachine(serial:String) {
	//creo el XML

	var msg:Message=new Message();
	msg.setAction(new ActionMachineLogIn());

	var register:Parameter=new Parameter();
	register.addItem("serial",serial);
	msg.setParameters(register);

	var str:String=msg.toXML();
			
	var doc:XML=new XML(str);
	/*
	Timer.newInstance(1500);
	Timer.getActive().addListener(this);
	Timer.getActive().beginTimer();
	*/
	//lo mando
	_root.txt_xml.text="Registrando maquina | IP: "+IP+" PORT: "+PORT;
	_root.txt_xml.text+="\n\n"+doc;
	send(doc);
		
}; //fin función

public function getGames():Void {
	
	var msg:Message=new Message();
	msg.setAction(new ActionMachineGetGames());

	var str:String=msg.toXML();
			
	var doc:XML=new XML(str);
	
	send(doc);
	
}

public function gameLogIn(serial:String):Void {
	//trace("Communication.as | gameLogIn serial= "+serial);
	var msg:Message=new Message();
	msg.setAction(new ActionGameLogIn());

	var register:Parameter=new Parameter();
	register.addItem("serial",serial);
	msg.setParameters(register);

	var str:String=msg.toXML();
			
	var doc:XML=new XML(str);
	/*
	Timer.newInstance(1500);
	Timer.getActive().addListener(this);
	Timer.getActive().beginTimer();
	*/
	send(doc);
	
}; //fin función

public function gameLogOut(serial:String):Void {
	//trace("Communication.as | gameLogIn serial= "+serial);
	var msg:Message=new Message();
	msg.setAction(new ActionGameLogOut());

	var register:Parameter=new Parameter();
	register.addItem("serial",serial);
	msg.setParameters(register);

	var str:String=msg.toXML();
			
	var doc:XML=new XML(str);
	/*
	Timer.newInstance(1500);
	Timer.getActive().addListener(this);
	Timer.getActive().beginTimer();
	*/
	send(doc);
	
}; //fin función

public function gameInit():Void {
	//trace("Communication.as | gameInit");
	var msg:Message=new Message();
	msg.setAction(new ActionGameInit());

	var str:String=msg.toXML();
			
	var doc:XML=new XML(str);
	
	send(doc);
	
}; //fin función

public function gamePlay(bet:Bet):Void {
	//trace("Communication.as | gamePlay bet= "+bet);
		
	var msg:Message=new Message();
	msg.setAction(new ActionGamePlay());
		
	var gameBet:Parameter=new Parameter();
	gameBet.addItem("bet", bet.getTokens());
	msg.setParameters(gameBet);

	var str:String=msg.toXML();
			
	var doc:XML=new XML(str);
	/*
	Timer.newInstance(1500);
	Timer.getActive().addListener(this);
	Timer.getActive().beginTimer();
	*/
	send(doc);
	
}; //fin función


public function playerLogIn(serial:String):Void {
	//Mando el mensaje
	
	//creo el XML

	var msg:Message=new Message();
	msg.setAction(new ActionPlayerLogIn());

	var register:Parameter=new Parameter();
	register.addItem("creditCardCode",serial);
	msg.setParameters(register);

	var str:String=msg.toXML();
			
	var doc:XML=new XML(str);
	/*
	Timer.newInstance(1500);
	Timer.getActive().addListener(this);
	Timer.getActive().beginTimer();
	*/
	send(doc);
	
}

public function playerLogOut():Void {
	//Mando el mensaje
	
	//creo el XML

	var msg:Message=new Message();
	msg.setAction(new ActionPlayerLogOut());

	var str:String=msg.toXML();
			
	var doc:XML=new XML(str);
	/*
	Timer.newInstance(1500);
	Timer.getActive().addListener(this);
	Timer.getActive().beginTimer();
	*/

	send(doc);
	
}

public function getPlayerFounds():Void {
	//Mando el mensaje
	
	//creo el XML
	//trace("Communication.as | getPlayerFounds ");
	var msg:Message=new Message();
	msg.setAction(new ActionPlayerGetFounds());

	var str:String=msg.toXML();
			
	var doc:XML=new XML(str);
	/*
	Timer.newInstance(1500);
	Timer.getActive().addListener(this);
	Timer.getActive().beginTimer();
	*/
	send(doc);
	
}

function onXML(doc:XML) {
	
	//_root.txt_xml.text=doc;
	//trace("---\n---XML\n\n"+doc+"\n\n----");
	//Timer.getActive().finishTimer();
	
	try{
		listener.event(CommunicationEventFactory.getComunicationEvent(doc));
	} catch (e:Error) {
		trace("Communication.as | 247 | error listener.event(CommunicationEventFactory.getComunicationEvent(doc));"+e);
		//listener.eventException(new CommunicationEventDisabled()));
	};
	
}

function onConnect(success:Boolean){
	if(!success){
		//Timer.getActive().finishTimer();
		trace("Communication.as | 256 | onConnectFail()");
		listener.eventException(new CommunicationFailException());
	} else {
		//TODO esto es bastante asqueroso
		registerMachine(SGS.getActive().getSerial());
	};
};

function onClose(){
	trace("Communication.as | 234 | onClose()");
	listener.eventException(new CommunicationLostException());
};

function timeOut():Void{
	listener.eventException(new CommunicationTimeOutException());
};

	
} // fin de clase